﻿/*----------------------------------------------------------------
// Copyright (C) 2016 通通优品版权所有。
// 命名空间: TongTongMall.MallWeb.WebApi
// 类名：MallWebWebApiModule
// 功能描述：N/A
// 
// 创建标识：Roc.Lee(李鹏鹏) 2017/8/26 14:22:33 TEC-ROCLEE Roc.Lee

// 修改标识：
// 修改描述：
// 
//
//----------------------------------------------------------------*/
using Castle.MicroKernel.Registration;
using System;
using System.Linq;
using System.Reflection;
using System.Web.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using TongTongMall.Dependecy;
using TongTongMall.MallWeb.ApiRepository.Configuration;
using TongTongMall.MallWeb.Application.Configuration;
using TongTongMall.MallWeb.Application.Configuration.Session;
using TongTongMall.MallWeb.WebApi.Configuration;
using TongTongMall.MallWeb.WebApi.Core;
using TongTongMall.MallWeb.WebApi.Core.Converters;
using TongTongMall.MallWeb.WebApi.Filter;
using TongTongMall.Modules;
using TongTongMall.WebApi.WebApi;
using TongTongMall.WebApi.WebApi.Configuration;

namespace TongTongMall.MallWeb.WebApi
{
    /// <summary>
    /// 
    /// </summary>
    [DependsOn(typeof(WebApiModule))]

    public class MallWebWebApiModule : BaseModule
    {/// <summary>
     /// 
     /// </summary>
        public override void Initialize()
        {
            IocManager.RegisterAssemblyByConvention(Assembly.GetExecutingAssembly());
            var httpConfiguration = IocManager.Resolve<IWebApiModuleConfiguration>().HttpConfiguration;
            InitializeFormatters(httpConfiguration);


        }
        /// <summary>
        /// 
        /// </summary>
        public override void PreInitialize()
        {
            IocManager.IocContainer.Register(Component.For<ApiAuthorizationAttribute>().LifeStyle.Singleton);
            IocManager.Register<ITongTongMallStartupConfiguration, TongTongMallStartupConfiguration>();
            IocManager.RegisterIfNot<IMallWebSession, MallWebSession>();
        }
        /// <summary>
        /// 
        /// </summary>
        public override void PostInitialize()
        {
            Configuration.Modules.ApiRepository().OAuthUrl = ApplicationDefaultInstance.Instance.ApiConfig.OAuthUrl;


        }

        /// <summary>
        /// 定义全局一些格式化项
        /// </summary>
        /// <param name="httpConfiguration"></param>
        private static void InitializeFormatters(HttpConfiguration httpConfiguration)
        {
            //httpConfiguration.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new LowerCaseContractResolver();
            httpConfiguration.Formatters.JsonFormatter.SerializerSettings.Converters.Insert(0, new JsonResultDateTimeConverter());
            httpConfiguration.Services.Replace(typeof(IHttpControllerSelector), new HttpNotFoundAwareDefaultHttpControllerSelector(httpConfiguration));
            httpConfiguration.Services.Replace(typeof(IHttpActionSelector), new HttpNotFoundAwareControllerActionSelector());
        }



    }
}
